<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\Activity;


use App\Http\Controllers\Controller;
use App\Models\ActivityLuck;
use App\Models\ActivityLuckDetail;
use App\Models\ActivityLuckDetailLog;
use App\Models\Bomb;
use App\Models\Good;
use App\Models\GoodsStock;
use App\Models\Kill;
use App\Models\Order;
use App\Models\Red;
use App\Models\RedRole;
use App\Models\Supplier;
use App\Models\UserHome;
use App\Models\UserIntegralRedLog;
use App\Models\VipLog;
use App\Services\Manager\GoodsService;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class KillController extends Controller
{

    private $namespace = "App\Http\Controllers\Api\V1\Manager\Activity\KillController";

    public function index(Request $request)
    {
        $Kill = Kill::first();
        $goodsName = $request->goodsName;
        $pageSize = $request->pageSize;

        $sql = Good::select('id', 'name', 'image', 'price', 'killStartTime', 'killEndTime', 'idNum', 'killIndex')
            ->where('status', 40);
        if (isset($goodsName)) {
            $sql->where('name', 'like', "%$goodsName%");
        }
        $goodList = $sql->orderByDesc('killIndex')
            ->orderByDesc('killIndexTime')
            ->paginate($pageSize)
            ->toArray();
        $killList = $goodList['data'];
        foreach ($killList as $key => $value) {
            $killList[$key]['killDay'] = -1;
            if (isset($value['killEndTime'])) {
                $day = (strtotime($value['killEndTime']) - strtotime(date("Y-m-d H:i:s"))) / 86400;
                if ($day > 0) {
                    $killList[$key]['killDay'] = intval($day);
                }
            }
            $killList[$key]['image'] = path($value['image']);
        }
        $Kill['killList'] = $killList;
        $Kill['totalNum'] = $goodList['total'];
        return $this->apiResponse($Kill);
    }

    public function store(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $data = $request->only('detail', 'killStartTime', 'killEndTime');
//        if (!$data['detail']) {
//            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
//        }
        Kill::where('killId', '!=', 0)->update($data);
        Good::where('is_del', 0)->where('status', 40)->update([
            'killStartTime'=>$data['killStartTime'],
            'killEndTime'=>$data['killEndTime']
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/store";
        $title = "修改";
        $content = "修改了限时秒杀的规则为【" . $data['detail'] . "】,开始时间为【".$data['killStartTime']."】,结束时间为【".$data['killEndTime']."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "限时秒杀模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function update(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $kill = Kill::first();
        if ($kill['status']) {
            $content = "禁用";
            Kill::where('killId', '!=', 0)->update(['status' => 0]);
        } else {
            $num = Good::where('status', 40)->count();
            if(!$num){
                return $this->apiResponse('', config('errorLiu.KAIQIHUODONGZHISHAOXUYAOYIJ'));
            }
            $content = "启用";
            Kill::where('killId', '!=', 0)->update(['status' => 1]);
        }
        //添加管理员日志开始
        $url = $this->namespace . "/update";
        $title = "修改";
        $content .= "了限时秒杀";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "限时秒杀模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }


    public function killDetail(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->id;
        $data = $request->only('idNum');
        if (!isset($id) || !$id || !isset($data['idNum'])) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $Kill = Kill::first();
        $data['killStartTime'] = $Kill['killStartTime'] ?: null;
        $data['killEndTime'] = $Kill['killEndTime'] ?: null;
        $data['status'] = 40;
        $good = Good::where('id', $id)->first();
        $goodService = new GoodsService();
        $res = $goodService->verify($good, $id);
        if ($res == -1) {
            return $this->apiResponse('', config('errorLiu.GONGYINGSHANGYITINGZHIHEZ'));
        }
        if ($res == -2) {
            return $this->apiResponse('', config('errorLiu.GASHANGPINXUANCZEZHONGLIANL'));
        }
        if ($res == -3) {
            return $this->apiResponse('', config('errorLiu.GAISHANGPINZANWUGUIGE'));
        }
        if ($res == -4) {
            return $this->apiResponse('', config('errorLiu.GAISHANGPGUIGESEWEIWZ'));
        }
        if ($res == -5) {
            return $this->apiResponse('', config('errorLiu.SHANGPINGUIGEZANWUTUPIANQING'));
        }
        if ($res == -6) {
            return $this->apiResponse('', config('errorLiu.SHANGPINGWEITIANXIEHANSUIHUIBUHAN'));
        }
        if($good['status'] == 2){
            $data['killCreateTime'] = date("Y-m-d H:i:s");
        }
        Good::where('id', $id)->where('is_del', 0)->whereIn('status', [2, 40])->update($data);
        //添加管理员日志开始
        $url = $this->namespace . "/killDetail";
        $title = "修改";
        $content = "添加修改了限时秒杀的商品,商品id为【".$id."】,内容为【".json_encode($data)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "限时秒杀模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function killIndex(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        Good::where('id', $id)->update([
            'killIndex' => 1,
            'killIndexTime' => date("Y-m-d H:i:s")
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/killIndex";
        $title = "修改";
        $content = "置顶了id为【".$id."】的商品";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "限时秒杀模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function destroy(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        Good::where('id', $id)->update([
            'killIndex' => 0,
            'status' => 2
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/destroy";
        $title = "删除";
        $content = "删除了id为【".$id."】的商品";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "限时秒杀模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }
}